/**
 * @Author: Liu, Yunfeng
 * @DateTime: 2021/4/7 21:53
 * @Description: 两数相加
 */
public class addTwoNumbers {
    public static ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        int bouns = 0;
        ListNode current1 = l1;
        ListNode current2 = l2;
        int currentValue = 0;
        while (current1.next != null && current2.next != null) {
            currentValue = current1.val + current2.val + bouns;
            current1.val = currentValue % 10;
            bouns = currentValue / 10;
            current1 = current1.next;
            current2 = current2.next;
        }
        if (current1.next == null) {
            current1.next = current2.next;
        }
        currentValue = current1.val + current2.val + bouns;
        current1.val = currentValue % 10;
        bouns = currentValue / 10;
        while (current1.next != null) {
            current1 = current1.next;
            currentValue = current1.val + bouns;
            current1.val = currentValue % 10;
            bouns = currentValue / 10;
        }
        if (bouns == 1) {
            ListNode tail = new ListNode(1);
            tail.next = null;
            current1.next = tail;
        }
        return l1;
    }

    public static void main(String[] args) {
        int[] nums1 = {2, 4, 3};
        int[] nums2 = {5, 6, 4};
        ListNode l1 = ListNode.makeListNode(nums1);
        ListNode l2 = ListNode.makeListNode(nums2);
        ListNode.printListNode(addTwoNumbers(l1, l2));

    }
}
